<template>
  <div>
    <h1>找回密码</h1>
    请输入邮箱<input type="text" v-model="email">
    请输入验证码<input type="text" v-model="code">
    <input type="button" value="发送验证码"  @click="send_verify_email">
    请输入修改密码<input type="text" v-model="password">
    再次输入密码<input type="text" v-model="password1">
    <input type="button" value="修改" @click="gai">
  </div>
</template>

<script>
  export default {
    name: "zhaomima",
    data() {
      return {
        email: '',
        code: '',
        password: '',
        password1: ''
      }
    },
    methods: {
      send_verify_email() {
        // 叹号取反，判断如果用户没有输入邮箱，直接返回 ，什么都不干
        if (!this.email) {
          this.message = '请输入邮箱后发送验证码'
          return;
        }
        let re_email = this.email.match('[a-zA-Z0-9.-]+[@][a-zA-Z0-9]+([.][a-zA-Z]){1,2}')
        if (!re_email) {
          this.message = '邮箱格式不正确'
          return;
        }
        // 输入了邮箱的话，获取邮箱，发送axios请求给对应接口
        let form_data = new FormData();
        form_data.append('email', this.email);
        var data = {'email': this.email};
        this.axios.post('/verifyemail/', data).then(res => {
          console.log(res.data);
          if (res.data.code === 200) {
            this.message = res.data.msg;
            alert(res.data.msg)
          } else {
            this.message = res.data.msg;
            alert(res.data.msg)
          }
        })
      },
      gai() {
        let form_data = new FormData()
        form_data.append('email', this.email);
        form_data.append('password', this.password);
        form_data.append('password1', this.password1);
        form_data.append('code', this.code);

        this.axios.post('/zhaohui/', form_data).then(res => {
          console.log(res.data);
          if (res.data.code === 200) {
            alert(res.data.msg)
          } else {
            alert(res.data.msg)
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
